import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * A Servlet to help view jpg images
 *
 * @author James Chy
 */

public class UpdateDate extends PhotoServlet {

    String subtitle, table, thispage, maintenancepage;
    String code1;
    String title1;
/*
    ResultSet rs;
*/
    static final long serialVersionUID = 0;
    
    public void init(ServletConfig config) throws ServletException
    {
        super.init(config);
        subtitle="Date";
        table="dates";
        thispage="UpdateDate";
        maintenancepage="MaintainDates";
        code1="date";
        title1="Date";
    }
        
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();

        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

        out.println("<title>Update " + subtitle + "</title>");  
        out.println("</head>");
        out.println("<body>");

        String query;

        String uid=request.getParameter("selection");

        query = "SELECT uid, " + code1 + " " +
                "FROM " + table + " " +
                "WHERE uid=" + uid;
            
        if(showQuery)
            out.println("Query:<BR>" + query + "<BR>");

        ResultSet rs = executeQuery(query);

        out.println("<B>" + subtitle + "</B><BR>");

        try
   		{
            if (rs.next()) 
            {
            	
	                String value=rs.getString(code1);
	                String value1=rs.getString(code1);
                    	   
	                out.println("Change: " + value + " to <BR>");
		            out.println("<BR><BR>");
		            out.println("<FORM ACTION=\"" + thispage + "\" METHOD=\"POST\">");
		            out.println("<INPUT TYPE=HIDDEN NAME=UID VALUE=" + uid + ">");
		            out.println("<TABLE>");
		            out.println("<TR><TD>"+title1 + "</TD><TD><INPUT TYPE=TEXT NAME=TEXTINPUT1 VALUE=\"" + value1 + "\"></TD></TR>");
		            out.println("</TABLE>");
		            out.println("<INPUT TYPE=SUBMIT NAME=\"Update\" VALUE=\"Update\">");
		            out.println("</FORM>");
		            out.println("<FORM ACTION=\"" + maintenancepage + "\" METHOD=\"GET\">");
		            out.println("<INPUT TYPE=SUBMIT NAME=\"Cancel\" VALUE=\"Cancel\">");
		            out.println("</FORM>");
		    }
	    }
	    catch(SQLException sqle)
	    {
	        out.println("SQL Exception");
	    }

        out.println("</body>");
        out.println("</html>");

    }

    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/html");
        out = response.getWriter();
        
        out.println("<html>");
        out.println("<body bgcolor=\"white\">");
        out.println("<head>");

        out.println("<title>"+ subtitle +" Updated</title>");  
        out.println("</head>");
        out.println("<body>");

        if(showQuery)
        {
            for(Enumeration e=request.getParameterNames(); e.hasMoreElements(); )
            {
                String currentElement=e.nextElement().toString();
                String currentValues[]=request.getParameterValues(currentElement);
                for(int i=0; i<currentValues.length; i++)
                    out.println(currentElement + "[" + i + "]=" + 
                            currentValues[i] + "<BR>");
            }
        }

        String query;
        String uid=request.getParameter("UID");
        String value1=request.getParameter("TEXTINPUT1");

        query = "UPDATE " + table + " " +
                "SET " + code1 + "=\"" + value1 + "\" " +
        		"WHERE uid=" + uid;

        if(showQuery) out.println("Query: <BR>" + query + "<BR>");

        int count = executeUpdate(query);
        if(count <= 0)
        {
        	out.println("<H1>Error: Nothing updated</H1>");
        }
        
		out.println("<A HREF=" + maintenancepage + "><H3>Return to Previous Page</H3></A>");
	    out.println("</BODY></HTML>");
    }
               
}
